package com.example.blog.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.blog.entity.Comment;
import com.example.blog.entity.Reply;
import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.CommentMapper;
import com.example.blog.mapper.ReplyMapper;
import com.example.blog.service.CommentService;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private ReplyMapper replyMapper;
    
    @Autowired
    private BlogMapper blogMapper;
    
    @Override
    @Transactional
    public Map<String, Object> addComment(Comment comment) {
        Map<String, Object> map = new HashMap<>();
        
        try {
            // 设置评论时间
            comment.setCreateTime(new Date());
            
            // 设置状态
            comment.setIsDisabled(false);
            comment.setIsDeletedByUser(false);
            
            // 插入评论
            int result = commentMapper.insert(comment);
            if (result > 0) {
                // 更新博客评论数
                blogMapper.updateComments(comment.getBlogId(), 1);
                
                map.put("success", true);
                map.put("message", "评论成功");
                map.put("commentId", comment.getCommentId());
            } else {
                map.put("success", false);
                map.put("message", "评论失败");
            }
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "系统异常：" + e.getMessage());
        }
        
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> addReply(Reply reply) {
        Map<String, Object> map = new HashMap<>();
        
        try {
            // 获取评论信息
            Comment comment = commentMapper.selectByPrimaryKey(reply.getCommentId());
            if (comment == null) {
                map.put("success", false);
                map.put("message", "评论不存在");
                return map;
            }
            
            // 验证：不能回复自己的评论
            if (comment.getUserId().equals(reply.getUserId())) {
                map.put("success", false);
                map.put("message", "不能回复自己的评论");
                return map;
            }
            
            // 设置回复时间
            reply.setCreateTime(new Date());
            
            // 设置状态
            reply.setIsDisabled(false);
            reply.setIsDeletedByUser(false);
            
            // 插入回复
            int result = replyMapper.insert(reply);
            if (result > 0) {
                map.put("success", true);
                map.put("message", "回复成功");
            } else {
                map.put("success", false);
                map.put("message", "回复失败");
            }
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "系统异常：" + e.getMessage());
        }
        
        return map;
    }

    @Override
    @Transactional
    public boolean updateComment(Comment comment) {
        try {
            Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentId());
            if (originalComment == null) {
                return false;
            }
            
            // 保留原状态
            comment.setIsDisabled(originalComment.getIsDisabled());
            comment.setIsDeletedByUser(originalComment.getIsDeletedByUser());
            comment.setCreateTime(originalComment.getCreateTime());
            
            int result = commentMapper.updateByPrimaryKey(comment);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateReply(Reply reply) {
        try {
            Reply originalReply = replyMapper.selectByPrimaryKey(reply.getUserId(), reply.getCommentId());
            if (originalReply == null) {
                return false;
            }
            
            // 保留原状态
            reply.setIsDisabled(originalReply.getIsDisabled());
            reply.setIsDeletedByUser(originalReply.getIsDeletedByUser());
            reply.setCreateTime(originalReply.getCreateTime());
            
            int result = replyMapper.updateByPrimaryKey(reply);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteComment(Integer commentId) {
        try {
            Comment comment = commentMapper.selectByPrimaryKey(commentId);
            if (comment == null) {
                return false;
            }
            
            // 删除评论及其回复
            replyMapper.deleteByCommentId(commentId);
            int result = commentMapper.deleteByPrimaryKey(commentId);
            
            // 更新博客评论数
            if (result > 0) {
                blogMapper.updateComments(comment.getBlogId(), -1);
            }
            
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteReply(Integer userId, Integer commentId) {
        try {
            int result = replyMapper.deleteByPrimaryKey(userId, commentId);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean markCommentAsDeletedByUser(Integer commentId) {
        try {
            int result = commentMapper.markAsDeletedByUser(commentId);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean markReplyAsDeletedByUser(Integer userId, Integer commentId) {
        try {
            int result = replyMapper.markAsDeletedByUser(userId, commentId);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateCommentStatus(Integer commentId, Boolean isDisabled) {
        try {
            int result = commentMapper.updateDisableStatus(commentId, isDisabled);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateReplyStatus(Integer userId, Integer commentId, Boolean isDisabled) {
        try {
            int result = replyMapper.updateDisableStatus(userId, commentId, isDisabled);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Comment getCommentById(Integer commentId) {
        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        if (comment != null) {
            // 加载回复
            List<Reply> replies = getValidRepliesByCommentId(commentId);
            comment.setReplies(replies);
        }
        return comment;
    }

    @Override
    public Reply getReplyByUserIdAndCommentId(Integer userId, Integer commentId) {
        return replyMapper.selectByPrimaryKey(userId, commentId);
    }

    @Override
    public List<Comment> getCommentsByBlogId(Integer blogId) {
        List<Comment> comments = commentMapper.selectByBlogId(blogId);
        
        // 加载回复
        for (Comment comment : comments) {
            comment.setReplies(getValidRepliesByCommentId(comment.getCommentId()));
        }
        
        return comments;
    }

    @Override
    public List<Comment> getValidCommentsByBlogId(Integer blogId) {
        List<Comment> comments = commentMapper.selectValidCommentsByBlogId(blogId);
        
        // 加载回复
        for (Comment comment : comments) {
            comment.setReplies(getValidRepliesByCommentId(comment.getCommentId()));
        }
        
        return comments;
    }

    @Override
    public List<Reply> getRepliesByCommentId(Integer commentId) {
        return replyMapper.selectByCommentId(commentId);
    }

    @Override
    public List<Reply> getValidRepliesByCommentId(Integer commentId) {
        return replyMapper.selectValidRepliesByCommentId(commentId);
    }

    @Override
    public List<Comment> getCommentsByUserId(Integer userId) {
        return commentMapper.selectByUserId(userId);
    }

    @Override
    public List<Reply> getRepliesByUserId(Integer userId) {
        return replyMapper.selectByUserId(userId);
    }
} 